<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Asays extends Controller_Abase_Adefaultactions {

    public function showDefaultList() {
        $params = $this->params();
        $currentPage = $this->request->param('page');

        if ($this->request->param('page') == 1 || empty($currentPage)) {
            $this->offset = 0;
        }
        else {
            $this->offset = $this->request->param('page')*$this->_config->get('itemsPerPage')-$this->_config->get('itemsPerPage');
        }

        $this->template->right = View::factory('apages/amain', array(
            'title' => $params['main_title'],
            'content' => View::factory('apieces/abase/adefaultContent', array(
                'list' => View::factory('apieces/alist/asaysList', array(
                    'defaults' => $this->dataListDefaults(),
                    'data' => ORM::factory($this->request->controller())->order_by('id', 'DESC')->limit($this->_config->get('itemsPerPage'))->offset($this->offset)->with('work')->with('admin')->find_all(),
                )),
                'form' => $this->form,
                'pagination' => $this->pagination
            )),
            'title' => $params['main_title'],
            'search_indexes' => !empty($params['search_indexes']) ? $params['search_indexes'] : '',
            'table' => $params['table'],
            'model' => $params['model']
        ));
    }

    public function action_delete() {
        $params = $this->params();
        $this->obj = ORM::factory($this->request->controller(), $this->request->param('id'));

        if (is_file($params['fields']['image']['image_params']['dir'].$this->obj->image)) {
            unlink($params['fields']['image']['image_params']['dir'].$this->obj->image);
        }

        parent::action_delete();
    }

}